import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';

class HttpsClient {
  // 域名根目录
  static String baseUrl = "https://xiaomi.itying.com/";
  // 获取原始对象
  static Dio dio = Dio();

  // 构造函数
  HttpsClient() {
    // 配置接口根目录
    dio.options.baseUrl = baseUrl;
    // 配置超时时间
    dio.options.connectTimeout = const Duration(seconds: 5);
    // 接受数据超时时间
    dio.options.receiveTimeout = const Duration(seconds: 5);
  }

  // get请求
  Future get(apiUrl) async {
    try {
      var response = await dio.get(apiUrl);
      return response;
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
      return null;
    }
  }

  // psot请求
  Future post(String apiUrl, {Map? data}) async {
    try {
      var response = await dio.post(apiUrl, data: data);
      return response;
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
      return null;
    }
  }

  // 修改图片地址
  static replaeUrl(picUrl) {
    String tempUrl = baseUrl + picUrl;

    return tempUrl.replaceAll('\\', '/');
  }
}
